package com.szh.mysobproject.viewmodel

import android.app.Application
import androidx.lifecycle.*
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.blankj.utilcode.util.RegexUtils
import com.szh.mysobproject.model.RichList
import com.szh.mysobproject.model.UserBasicInfo
import com.szh.mysobproject.paging.source.RichPagingSource
import com.szh.mysobproject.viewmodel.app.Repository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch

/**
 * @author : create by  szh
 * @date : 2022/6/17 14:47
 * @desc :用户 ViewModel
 */

class UserViewModel(application: Application) : AndroidViewModel(application) {

    private val _userBasicInfo = MutableLiveData<UserBasicInfo?>()
    val userBasicInfo: LiveData<UserBasicInfo?> get() = _userBasicInfo

    private val phoneLiveData = MutableLiveData<String>()
    val userAvatarLiveData = Transformations.switchMap(phoneLiveData) { account ->
        Repository.queryUserAvatar(account)
    }

    /**
     * 获取富豪榜
     */
    fun getRichList(): Flow<PagingData<RichList.RichUserItem>> {
        return Pager(config = PagingConfig(30),
            pagingSourceFactory = { RichPagingSource() })
            .flow.cachedIn(viewModelScope)
    }

    /**
     * 通过账号（手机号）获取用户头像
     */
    fun queryUserAvatar(account: String) {
        phoneLiveData.value = account
    }

    /**
     * 获取用户信息
     */
    fun getUserInfo(userId: String) = Repository.getUserInfo(userId)

    /**
     * 自己与目标用户的关注状态
     *
     * 0：表示没有关注对方，可以显示为：关注
     * 1：表示对方关注自己，可以显示为：回粉
     * 2：表示已经关注对方，可以显示为：已关注
     * 3：表示相互关注，可以显示为：相互关注
     */
    fun followState(userId: String) = Repository.followState(userId)

    /**
     * 关注用户
     */
    fun followUser(userId: String) = Repository.followUser(userId)

    /**
     * 取消关注
     */
    fun unfollowUser(userId: String) = Repository.unFollowUser(userId)

    /**
     * 退出登录
     */
    fun logout() = Repository.logout()

    /**
     * 账号登录
     */
    fun login(userAccount: String, password: String, captcha: String) {
        viewModelScope.launch {
            val userInfoValid =
                userAccount.isUserAccountValid() && password.isPasswordValid() && captcha.isVerifyCodeValid()
            if (userInfoValid.not()) {
                _userBasicInfo.value = null
                return@launch
            }
            val userBasicInfo = Repository.login(userAccount, password, captcha)
            _userBasicInfo.value = userBasicInfo
        }
    }

    /**
     * 获取当前用户个人成就
     */
    fun getAchievement() = Repository.getAchievement()

    /**
     * 获取指定用户个人成就
     */
    fun getAchievement(userId: String) = Repository.getAchievement(userId)

    /**
     * 检查是否领取过津贴
     */
    fun checkAllowance() = Repository.checkAllowance()

    /**
     * 获取VIP用户列表
     */
    fun getVipUserList() = Repository.getVipUserList()

    /**
     * VIP每月领取津贴
     */
    fun getAllowance() = Repository.getAllowance()

    /**
     * 查询用户信息
     */
    fun queryUserInfo() = Repository.queryUserInfo()

    /**
     * 手机号格式检查
     */
    private fun String.isUserAccountValid(): Boolean = RegexUtils.isMobileExact(this)

    /**
     * 账号密码长度检查
     */
    private fun String.isPasswordValid(): Boolean = length > 5

    /**
     * 验证码检查
     */
    private fun String.isVerifyCodeValid(): Boolean = isNotEmpty()

}